'''
This is a demonstration program.

Describe data set with form
Age,Sex,BMI,Weight,Years Smoking

Give mean, median, var, range, iqr
'''

def calculateMedian(data):
    ''' Calculate the median of a list '''
    data.sort()
    
    if len(data) % 2 == 0:
        return (data[len(data) / 2 - 1] + data[len(data)/2]) / 2
    else:
        return  data[len(data) / 2]

def calculateDescriptiveStats(data):
    ''' Calculate mean, median, var, range for a set of data '''
    expSquares = 0.0
    exp = 0.0
    for n in data:
        exp += n
        expSquares += n*n

    expSquares /= len(data)
    exp /= len(data)
    variance = expSquares - exp*exp
    median = calculateMedian(data)
    minData = min(data)
    maxData = max(data)

    return (exp, variance, median, minData, maxData)

#####################################################################

import sys
if len(sys.argv) < 2:
    print "Usage: python describe_data.py <file>" 
    sys.exit(1)


fileName = sys.argv[1]
fileHandle = open(fileName,'r')

header = fileHandle.readline()

age = []
gender = []
bmi = []
weight = []
yrsSmoking = []

ageM = []
genderM = []
bmiM = []
weightM = []
yrsSmokingM = []

ageF = []
genderF = []
bmiF = []
weightF = []
yrsSmokingF = []

lineNum = 1
for line in fileHandle:
    # tokenize
    tokens = line.rstrip().split(',')
    age.append(float(tokens[0]))
    gender.append(tokens[1])
    bmi.append(float(tokens[2]))
    weight.append(float(tokens[3]))
    yrsSmoking.append(float(tokens[4]))

    if tokens[1] == 'M':

        ageM.append(float(tokens[0]))
        genderM.append(tokens[1])
        bmiM.append(float(tokens[2]))
        weightM.append(float(tokens[3]))
        yrsSmokingM.append(float(tokens[4]))

    elif tokens[1] == 'F':

        ageF.append(float(tokens[0]))
        genderF.append(tokens[1])
        bmiF.append(float(tokens[2]))
        weightF.append(float(tokens[3]))
        yrsSmokingF.append(float(tokens[4]))

    else:
        print "Error in data, line ", lineNum
        sys.exit(1)

    lineNum+=1

ageStats = calculateDescriptiveStats(age)
ageStatsM = calculateDescriptiveStats(ageM)
ageStatsF = calculateDescriptiveStats(ageF)

bmiStats = calculateDescriptiveStats(bmi)
bmiStatsM = calculateDescriptiveStats(bmiM)
bmiStatsF = calculateDescriptiveStats(bmiF)

weightStats = calculateDescriptiveStats(weight)
weightStatsM = calculateDescriptiveStats(weightM)
weightStatsF = calculateDescriptiveStats(weightF)

smokeStats = calculateDescriptiveStats(yrsSmoking)
smokeStatsM = calculateDescriptiveStats(yrsSmokingM)
smokeStatsF = calculateDescriptiveStats(yrsSmokingF)


print "N: %d %d %d" %(len(age), len(ageM), len(ageF))

print "Age:"
print "\tmean %f %f %f" % (ageStats[0], ageStatsM[0], ageStatsF[0])
print "\tmedian %f %f %f" % (ageStats[2], ageStatsM[2], ageStatsF[2])
print "\tvar %f %f %f" % (ageStats[1], ageStatsM[1], ageStatsF[1])
print "\trange total: %f to %f" % (ageStats[3],ageStats[4])
print "\trange male: %f to %f" % (ageStatsM[3],ageStatsM[4])
print "\trange female: %f to %f" % (ageStatsF[3],ageStatsF[4])
print "BMI:"
print "\tmean %f %f %f" % (bmiStats[0], bmiStatsM[0], bmiStatsF[0])
print "\tmedian %f %f %f" % (bmiStats[2], bmiStatsM[2], bmiStatsF[2])
print "\tvar %f %f %f" % (bmiStats[1], bmiStatsM[1], bmiStatsF[1])
print "\trange total: %f to %f" % (bmiStats[3],bmiStats[4])
print "\trange male: %f to %f" % (bmiStatsM[3],bmiStatsM[4])
print "\trange female: %f to %f" % (bmiStatsF[3],bmiStatsF[4])
print "Weight:"
print "\tmean %f %f %f" % (weightStats[0], weightStatsM[0], weightStatsF[0])
print "\tmedian %f %f %f" % (weightStats[2], weightStatsM[2], weightStatsF[2])
print "\tvar %f %f %f" % (weightStats[1], weightStatsM[1], weightStatsF[1])
print "\trange total: %f to %f" % (weightStats[3],weightStats[4])
print "\trange male: %f to %f" % (weightStatsM[3],weightStatsM[4])
print "\trange female: %f to %f" % (weightStatsF[3],weightStatsF[4])
print "Years smoking:"
print "\tmean %f %f %f" % (smokeStats[0], smokeStatsM[0], smokeStatsF[0])
print "\tmedian %f %f %f" % (smokeStats[2], smokeStatsM[2], smokeStatsF[2])
print "\tvar %f %f %f" % (smokeStats[1], smokeStatsM[1], smokeStatsF[1])
print "\trange total: %f to %f" % (smokeStats[3],smokeStats[4])
print "\trange male: %f to %f" % (smokeStatsM[3],smokeStatsM[4])
print "\trange female: %f to %f" % (smokeStatsF[3],smokeStatsF[4])


